<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$ss = $params['module_data'][$this->keyname];

foreach($ss as $k=>$v){

    if($DB->countRows($CONFIG->dbprefix.'jfxcss_stylesheets', "keyname = '{$v['keyname']}'")==0){
    $dbParams = array(
            'keyname' => $v['k'],
            'is_default' => $v['d'],
            'browser_version' => $v['v'],
            'sorting' => $v['sorting']
        );
        $cssid = $DB->insert($CONFIG->dbprefix.'jfxcss_stylesheets', $dbParams);

    }else{

        $sParams = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}stylesheets WHERE keyname = '{$v['k']}'");
        $cssid = $sParams['id'];

    }
    $fh = fopen($this->dirname.'/files/'.$cssid.'.css', 'w');
    fwrite($fh, $v['c']);
    fclose($fh);

    $sorting = (int) $DB->oneValue($CONFIG->dbprefix.'jfxcss_pages_to_stylesheets', 'sorting', "page_id = '{$params['page_id']}' ORDER BY sorting DESC");
    $sorting++;

    $dbParams = array(
        'page_id' => $params['page_id'],
        'css_key' => $v['k'],
        'sorting' => $sorting
    );
    $DB->insert($CONFIG->dbprefix.'jfxcss_pages_to_stylesheets', $dbParams);

    foreach($v['l'] as $langkey=>$value){
        $this->updateContentLang('title', $v['k'], $value, $langkey, '*');
    }

}